<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            height: 300px;
            background: skyblue;
        }
    </style>
</head>
<body>
    <div class="box">我是一条广告,<span id="msg">5</span>后关闭</div>
    <script>
        var oBox=document.querySelector(".box");
        var oMsg=document.getElementById("msg");


        var num=5;
        方式1
        var timer=setInterval(function(){
            num--;
            oMsg.innerHTML=num;
            if(num<0){
                clearInterval(timer);
                oBox.style.display="none";
            }
        },1000)


        // 方式2
        // function fn(){
        //     setTimeout(fn(),)
        // }
    </script>
</body>
</html>